﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.WizardAgent
{
    public class CarEquipmentRequest : IRequest
    {
        #region 取还车信息
        /// <summary>
        /// 取车时间
        /// </summary>
        public DateTime PickupDateTime { get; set; }

        /// <summary>
        /// 取车门店编号
        /// </summary>
        public string PickUpStoreCode { get; set; }

        /// <summary>
        /// 还车时间
        /// </summary>
        public DateTime ReturnDateTime { get; set; }

        /// <summary>
        /// 还车门店编号
        /// </summary>
        public string ReturnStoreCode { get; set; }
        #endregion

        public string AWD { get; set; }
        public string WizardNum { get; set; }
        public string CouponNum { get; set; }
        public string IATA { get; set; }
        public int IATAType { get; set; }

        /// <summary>
        /// 套餐价格代码
        /// </summary>
        public string RateCode { get; set; }

        /// <summary>
        /// 预订车组的唯一码
        /// </summary>
        public string SIPPCode { get; set; }

        /// <summary>
        /// 公司（旅行社、携程）的折扣号的XML
        /// </summary>
        /// <returns></returns>
        protected string XCorpDiscountNmbr
        {
            get
            {
                var xmlCorpDiscountNum = string.Empty;
                if (!string.IsNullOrEmpty(RateCode) || !string.IsNullOrEmpty(CouponNum) || !string.IsNullOrEmpty(AWD))
                {
                    var rateCategory = 6;
                    var xmlProp = "";

                    // 套餐号
                    if (!string.IsNullOrEmpty(RateCode))
                    {
                        rateCategory = 2;
                        xmlProp += string.Format(" RateQualifier='{0}' ", RateCode);

                    }

                    // 优惠券号
                    if (!string.IsNullOrEmpty(CouponNum))
                    {
                        xmlProp += string.Format(" PromotionCode='{0}' ", CouponNum);
                    }

                    if (!string.IsNullOrEmpty(AWD))
                    {
                        xmlProp += string.Format(" CorpDiscountNmbr='{0}' ", AWD);
                    }

                    xmlCorpDiscountNum = string.Format("<RateQualifier {0} RateCategory='{1}'/>", xmlProp, rateCategory);
                }

                return xmlCorpDiscountNum;
            }
        }

        /// <summary>
        /// 协议号的XML
        /// </summary>
        protected string XMLTATAANN
        {
            get
            {
                string xmlIATAANN = string.Empty;
                if (!string.IsNullOrEmpty(IATA))
                {
                    xmlIATAANN = string.Format("<Source><RequestorID ID='{0}' Type='{1}'/></Source>", IATA, IATAType);
                }
                return xmlIATAANN;
            }
        }

        public string GetCacheKey()
        {
            return string.Empty;
        }

        public string ToRequestXml()
        {
            var request = this;
            string xmlRequest = string.Format(WizardConsts.OrderProductListXML
                        , request.XMLTATAANN
                        , request.PickupDateTime
                        , request.ReturnDateTime
                        , request.PickUpStoreCode
                        , request.ReturnStoreCode
                        , request.SIPPCode
                        , request.XCorpDiscountNmbr
                    )
                    .Replace("'", "\"")
                    .Replace("OutboundUser", WizardConsts.OutboundUser)
                    .Replace("OutboundPwd", WizardConsts.OutboundPwd)
                    .Replace("OutboundRequestID", WizardConsts.OutboundRequestID);

            return xmlRequest;
        }

        public bool CheckRequest()
        {
            if (this.PickupDateTime.Ticks == 0)
            {
                throw new WizardException("无效的取车日期");
            }

            if (this.ReturnDateTime.Ticks == 0)
            {
                throw new WizardException("无效的还车日期");
            }

            if (string.IsNullOrWhiteSpace(this.PickUpStoreCode))
            {
                throw new WizardException("取车门店不能为空");
            }

            if (string.IsNullOrWhiteSpace(this.ReturnStoreCode))
            {
                throw new WizardException("还车门店不能为空");
            }

            if (string.IsNullOrWhiteSpace(SIPPCode))
            {
                throw new WizardException("预订车组的唯一码不能为空！");
            }

            if (string.IsNullOrWhiteSpace(RateCode))
            {
                throw new WizardException("套餐价格代码不能为空！");
            }

            return true;
        }
    }
}
